﻿Imports System.Data
Imports MyGeneration.dOOdads
Public Class rc_Depositos
    Private _MessageError As Exception = Nothing
    Public Property MessageError() As Exception
        Get
            Return _MessageError
        End Get
        Set(ByVal value As Exception)
            _MessageError = value
        End Set
    End Property
#Region "Depositos"
    Public Function Insert_Deposito(ByVal conn As String, ByVal descripcion As String, ByVal ubicacion As String, ByVal observacion As String) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim deposito As New Class_RC_Data.Class_RC.DEPOSITOS(conn)

            deposito.AddNew()
            deposito.DescripcionDep = descripcion
            deposito.UbicacionDep = ubicacion
            deposito.ObsDeposito = observacion
            deposito.Save()
            tx.CommitTransaction()

            Return deposito.CodDeposito

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_Deposito(ByVal conn As String, ByVal coddeposito As Integer, ByVal descripcion As String, ByVal ubicacion As String, ByVal observacion As String) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim deposito As New Class_RC_Data.Class_RC.DEPOSITOS(conn)
            deposito.Where.CodDeposito.Value = coddeposito

            If deposito.Query.Load Then

                deposito.DescripcionDep = descripcion
                deposito.UbicacionDep = ubicacion
                deposito.ObsDeposito = observacion
                deposito.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Deposito(ByVal conn As String, ByVal coddeposito As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim deposito As New Class_RC_Data.Class_RC.DEPOSITOS(conn)
            deposito.Where.CodDeposito.Value = coddeposito

            If deposito.Query.Load Then

                deposito.MarkAsDeleted()
                deposito.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_Depositos_All(ByVal conn As String, ByVal coddeposito As Integer, ByVal descripcion As String) As DataTable
        Try

            Dim deposito As New Class_RC_Data.Class_RC.DEPOSITOS(conn)

            If coddeposito > 0 Then
                deposito.Where.CodDeposito.Value = coddeposito
            End If

            If descripcion <> "" Then
                deposito.Where.DescripcionDep.Value = "%" & descripcion & "%"
                deposito.Where.DescripcionDep.Operator = WhereParameter.Operand.Like
            End If

            If deposito.Query.Load Then
                If deposito.DefaultView.Table.Rows.Count > 0 Then
                    Return deposito.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function
#End Region
#Region "Secciones"
    Public Function Insert_Seccion(ByVal conn As String, ByVal descripcion As String, ByVal codDeposito As Integer, ByVal observacion As String) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim Seccion As New Class_RC_Data.Class_RC.SECCIONES(conn)

            Seccion.AddNew()
            Seccion.DescripcionSeccion = descripcion
            Seccion.CodDeposito = codDeposito
            Seccion.ObsSeccion = observacion
            Seccion.Save()
            tx.CommitTransaction()

            Return Seccion.CodSeccion

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_Seccion(ByVal conn As String, ByVal codSeccion As Integer, ByVal descripcion As String, ByVal codDeposito As Integer, ByVal observacion As String) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim Seccion As New Class_RC_Data.Class_RC.SECCIONES(conn)
            Seccion.Where.CodSeccion.Value = codSeccion

            If Seccion.Query.Load Then

                Seccion.DescripcionSeccion = descripcion
                Seccion.CodDeposito = codDeposito
                Seccion.ObsSeccion = observacion
                Seccion.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Seccion(ByVal conn As String, ByVal codSeccion As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim Seccion As New Class_RC_Data.Class_RC.SECCIONES(conn)
            Seccion.Where.CodSeccion.Value = codSeccion

            If Seccion.Query.Load Then

                Seccion.MarkAsDeleted()
                Seccion.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_Secciones_All(ByVal conn As String, ByVal codSeccion As Integer, ByVal descripcion As String, ByVal codDeposito As Integer) As DataTable
        Try

            Dim Seccion As New Class_RC_Data.Class_RC.VistaSecciones(conn)

            If codSeccion > 0 Then
                Seccion.Where.CodSeccion.Value = codSeccion
            End If

            If codDeposito > 0 Then
                Seccion.Where.CodDeposito.Value = codDeposito
            End If

            If descripcion <> "" Then
                Seccion.Where.DescripcionSeccion.Value = "%" & descripcion & "%"
                Seccion.Where.DescripcionSeccion.Operator = WhereParameter.Operand.Like
            End If

            If Seccion.Query.Load Then
                If Seccion.DefaultView.Table.Rows.Count > 0 Then
                    Return Seccion.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function
#End Region

End Class
